<template>
  <br>
  <h5 style="text-align: center">所有枚举管理</h5>
  <br>
  <el-button @click="addClassEnum">增加枚举分类</el-button>

  <br>
  <br>
  <el-scrollbar height="76vh">
    <div style="text-align: center">
      <div style="width: 60em;margin: auto">
        <!--  chatGPT自动生成  -->
        <el-collapse v-model="activeNames">
          <el-collapse-item v-for="(item, index) in allEnumList" :title="item.enumName"
                            :key="index" :name="index.toString()">
            <el-button size="small" style="margin-bottom: 6px" @click="handleEdit(item)">修改分类信息</el-button>
            <el-table :data="item.children" style="width: 100%">
              <el-table-column prop="enumValue" label="枚举值" width="80"></el-table-column>
              <el-table-column prop="enumName" label="枚举名称" width="180"></el-table-column>
              <el-table-column prop="enumDesc" label="枚举描述" width="300"></el-table-column>
              <el-table-column prop="enumType" label="枚举类型" width="180"></el-table-column>
              <el-table-column prop="enableFlag" label="是否启用" width="80"></el-table-column>
              <el-table-column label="操作" width="140" fixed="right">
                <template #default="scope">
                  <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
                  <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-button size="small" @click="handleAdd(item)">增加枚举</el-button>
          </el-collapse-item>
        </el-collapse>
      </div>
    </div>
  </el-scrollbar>

  <!-- chatGPT生成 -->
  <el-dialog :title="enumUpsetTitle" v-model="editVisible" width="30%">
    <el-form :model="enumOne" :rules="editRules" ref="editFormRef" style="margin: 0 14px" label-width="80px">
      <el-form-item label="枚举名称" prop="enumName">
        <el-input v-model="enumOne.enumName" type="text" placeholder="请输入名称" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="枚举值" prop="enumValue">
        <el-input v-model="enumOne.enumValue" type="number" placeholder="请输入值"></el-input>
      </el-form-item>
      <el-form-item label="枚举说明" prop="enumValue">
        <el-input v-model="enumOne.enumDesc" type="textarea" placeholder="请输入枚举寿命"></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="enableFlag">
        <el-switch v-model="enumOne.enableFlag" :active-value="1" :inactive-value="0"
                   active-text="启用" inactive-text="停用"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="editVisible = false">取消</el-button>
      <el-button type="primary" @click="onEditSubmit">确定</el-button>
    </div>
  </el-dialog>

  <el-dialog title="增加枚举分类" v-model="addClassEnumVisible" width="60%">
    <h5 style="text-align: center">枚举分类</h5>
    <el-row align="middle" justify="center">
      <el-col :span="6">
        <el-row align="middle" justify="center">
          <el-col :span="6">分类名称</el-col>
          <el-col :span="18">
            <el-input v-model="enumOne.enumName"/>
          </el-col>
        </el-row>
      </el-col>

      <el-col :span="6">
        <el-row align="middle" justify="center">
          <el-col :span="6">分类值</el-col>
          <el-col :span="18">
            <el-input v-model="enumOne.enumValue"/>
          </el-col>
        </el-row>
      </el-col>

      <el-col :span="6">
        <el-row align="middle" justify="center">
          <el-col :span="6">分类说明</el-col>
          <el-col :span="18">
            <el-input v-model="enumOne.enumDesc"/>
          </el-col>
        </el-row>
      </el-col>

      <el-col :span="6">
        <el-row align="middle" justify="center">
          <el-col :span="6">是否启用</el-col>
          <el-col :span="18">
            <el-switch v-model="enumOne.enableFlag" :inactive-value="0" :active-value="1" inactive-text="停用"
                       active-text="启用"/>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <hr>
    <h5 style="text-align: center">枚举</h5>
    <el-row v-for="item in enumOne.children" align="middle" justify="center">
      <el-col :span="6">
        <el-row align="middle" justify="center">
          <el-col :span="6">分类名称</el-col>
          <el-col :span="18">
            <el-input v-model="item.enumName"/>
          </el-col>
        </el-row>
      </el-col>

      <el-col :span="6">
        <el-row align="middle" justify="center">
          <el-col :span="6">分类值</el-col>
          <el-col :span="18">
            <el-input v-model="item.enumValue"/>
          </el-col>
        </el-row>
      </el-col>

      <el-col :span="6">
        <el-row align="middle" justify="center">
          <el-col :span="6">分类说明</el-col>
          <el-col :span="18">
            <el-input v-model="item.enumDesc"/>
          </el-col>
        </el-row>
      </el-col>

      <el-col :span="6">
        <el-row align="middle" justify="center">
          <el-col :span="6">是否启用</el-col>
          <el-col :span="18">
            <el-switch v-model="item.enableFlag" :inactive-value="0" :active-value="1" inactive-text="停用"
                       active-text="启用"/>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <el-button @click="addChildrenEnum" size="small">增加</el-button>
    <div slot="footer">
      <el-button @click="addClassEnumVisible = false">取消</el-button>
      <el-button type="primary" @click="onEditSubmit">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import myaxios from "@/plugins/myaxios";
import SystemConstants from "@/utils/SystemConstants";
import {ElMessage} from "element-plus";
import CommonUtils from "@/utils/CommonUtils";
import CodeTest from "@/views/test/CodeTest";

export default {
  name: "AllEnum",
  components: {CodeTest},
  data() {
    return {
      activeNames: [],
      allEnumDemo: SystemConstants.getOneEnumClass(),
      allEnumList: [SystemConstants.getOneEnumClass()],
      enumUpsetTitle: "修改枚举值",
      code: 'test',
      enumOne: {},
      editVisible: false,
      editRules: {
        enumName: [
          {required: true, message: '请输入enumName', trigger: 'blur'},
          {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
        ]
      },
      addClassEnumVisible: false,
      parentEnum: {}
    }
  },
  methods: {
    findAll() {
      myaxios.get('/dist/system/getAllEnum').then(rep => {
        if (rep.data.code !== 100) {
          ElMessage.error(rep.data.msg)
          return
        }
        this.allEnumList = rep.data.data.enumClassList
        this.allEnumDemo = rep.data.data.enumClassDemo
        console.log(this.allEnumList)
      }, err => {

      })
    },
    handleAdd(row) {
      this.parentEnum = row
      this.enumUpsetTitle = "增加枚举值"
      this.enumOne = this.allEnumDemo.children[0]
      this.enumOne.enumType = row.id
      this.enumOne.id = null
      this.enumOne.parentId = row.id
      this.editVisible = true
    },
    handleEdit(row) {
      this.enumUpsetTitle = "修改枚举值"
      this.enumOne = row
      this.editVisible = true
    },
    handleDelete(row) {
      myaxios.delete('/dist/system/allEnum/' + row.id).then(rep => {
        if (rep.data.code !== 100) {
          ElMessage.error(rep.data.msg)
          return
        }
        ElMessage.success(row.enumName + ' 删除成功。')
        this.findAll()
      }, err => {
        ElMessage.error(err.response.data)
      })
    },
    onEditSubmit() {
      // 发起请求
      myaxios.post('/dist/system/upsertEnum', this.enumOne).then(rep => {
        if (rep.data.code !== 100) {
          ElMessage.error(rep.data.msg)
          return
        }
        ElMessage.success(this.enumUpsetTitle + '成功。')
        this.editVisible = false
        this.addClassEnumVisible = false
        this.findAll()
      }, err => {
        ElMessage.error(err.response.data)
      })
    },
    addClassEnum() {
      this.enumOne = CommonUtils.cloneObj(this.allEnumDemo)
      this.enumOne.children = []
      this.parentEnum = null
      this.addClassEnumVisible = true
    },
    addChildrenEnum() {
      this.enumOne.children.push(CommonUtils.cloneObj(this.allEnumDemo.children[0]))
      console.log(this.enumOne)
    },
    test() {
      console.log('test')
    }
  },
  mounted() {
    this.findAll()
  }
}
</script>

<style scoped>

</style>